
function listMedicineReceipt() {
	// TODO: validate 'monthTxt'
	$.get(
			'../MedicineManagement/listMedicineReceipt?set_ajax_view',
			{
			month_year: $('#monthTxt').val()
			},
			function(data) {
				$('select#medicineReceiptSearchLbx').html("<option id='empty' value='empty'>" + _("Select receipt") + "</option>");
				$('select#medicineReceiptSearchLbx').append(data);
			}
		);	
}

function getMedicineReceipt() {
	var receiptId = $('select#medicineReceiptSearchLbx option:selected').val();
	var medTypeId = $('select#medicineTypeSearchLbx option:selected').val()
	callMedicineReceiptAjax(receiptId, medTypeId);
}

function callMedicineReceiptAjax(receiptId, medTypeId) {
	$.get(
			'../MedicineManagement/getMedicineReceipt?set_ajax_view',
			{
			receiptId: receiptId,
			medTypeId: medTypeId
			},
			function(data) {
				$('table#addMedicineReceiptTbl tbody').html('');
				if (data == '') {
					$('table#addMedicineReceiptTbl tbody').append(_("Result not found."));
					$('#addMedReceiptBtn').attr('disabled', 'true');
				} else {
					$('table#addMedicineReceiptTbl tbody').append(data);
					$('#addMedReceiptBtn').removeAttr('disabled');
				}
			}
		);
}

function addOrUpdateMedReceipt() {	
	var medReceiptId = $('#medicine_receipt_id').val();
	$('#addMedReceiptBtn').hide();
	$('#addingTemplate').show();
	$.post (
			'../MedicineManagement/addOrUpdateMedReceipt?set_ajax_view',
			{			
			medReceiptId: medReceiptId,
			rowIds: $('#row_id_list').val(),
			receiptDetailForm: $('#addMedicineReceiptForm').serialize()
			},
			function(data) {
				$('#addMedReceiptBtn').show();
				$('#addingTemplate').hide();
				if (medReceiptId == '') {
					// create mode, load the content of medicine receipt table again
					callMedicineReceiptAjax(data, 'empty');
				}
				// print medicine receipt
				var url='../MedicineManagement/printMedicineReceipt?set_print_view&medReceiptId=' + data;				    
		        window.open(url, '', 'scrollbars=yes, menubar=no, width=800, height=600, resizable=yes, toolbar=no, location=no, status=no');
			}
		);
}
